﻿using System;
using System.Runtime.CompilerServices;

namespace 获取调用成员的特性
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass();
            //myClass.MyProperty = "Hello"; // 调用 SetMyProperty 方法时，会自动传递 memberName 参数为 "MyClass.MyProperty"
            myClass.SetMyProperty("world");
        }
    }


    public class MyClass
    {
        public string MyProperty { get; set; }
    }

    public static class MyExtensions
    {
        public static void SetMyProperty(this MyClass target, string value, 
            [CallerMemberName] string? memberName = null,
            [CallerFilePath] string? filePath = null,
            [CallerLineNumber] int? lineNumber = 0)
        {
            // 在这里可以使用 memberName 来获取调用属性的成员的名称  
            Console.WriteLine($"调用者 {memberName}.MyProperty to {value}");
            Console.WriteLine($"文件路径 {filePath}");
            Console.WriteLine($"行号 {lineNumber}");
            target.MyProperty = value;
        }
    }
}
